package org.example.commodity.service;

import lombok.extern.slf4j.Slf4j;
import org.example.commodity.api.CommodityAPI;
import org.example.commodity.data.entity.CommodityEntity;
import org.example.commodity.data.form.CommodityInfoForm;
import org.example.commodity.data.view.CommodityInfo;
import org.example.commodity.service.stroage.CommodityEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class CommodityService implements CommodityAPI {

    @Autowired
    private CommodityEntityMapper commodityEntityMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public CommodityInfo addCommodity(CommodityInfoForm commodityCreateForm) {

        CommodityEntity commodityEntity = transformFrom(commodityCreateForm);

        Integer result = commodityEntityMapper.insertCommodityEntity(commodityEntity);

        if(1 != result){
            return null;

        }

        return transformTo(commodityEntity);

    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public CommodityInfo getCommodity(CommodityInfo commodityInfo) {

        CommodityEntity commodityEntity = commodityEntityMapper.selectCommodityEntityById(commodityInfo.getId());

        if(null == commodityEntity){
            return null;
        }

        return transformTo(commodityEntity);

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public CommodityInfo updateCommodity(CommodityInfoForm commodityInfo) {

        CommodityEntity commodityEntity = transformFrom(commodityInfo);

        Integer result = commodityEntityMapper.updateCommodityEntity(commodityEntity);

        if(1 != result){
            return null;

        }

        CommodityEntity updated = commodityEntityMapper.selectCommodityEntityById(commodityEntity.getId());

        return transformTo(updated);

    }

    private CommodityEntity transformFrom(CommodityInfo commodityInfo){

        CommodityEntity commodityEntity = new CommodityEntity();

        commodityEntity.setId(commodityInfo.getId());
        commodityEntity.setCode(commodityInfo.getCode());
        commodityEntity.setName(commodityInfo.getName());
        commodityEntity.setImage(commodityInfo.getImage());
        commodityEntity.setPrice(commodityInfo.getPrice());
        commodityEntity.setState(commodityInfo.getState());
        commodityEntity.setCreateTimestamp(commodityInfo.getCreateTimestamp());

        return commodityEntity;

    }


    private CommodityEntity transformFrom(CommodityInfoForm commodityCreateForm){

        CommodityEntity commodityEntity = new CommodityEntity();

        commodityEntity.setCode(commodityCreateForm.getCode());
        commodityEntity.setName(commodityCreateForm.getName());
        commodityEntity.setImage(commodityCreateForm.getImage());
        commodityEntity.setPrice(commodityCreateForm.getPrice());
        commodityEntity.setState(commodityCreateForm.getState());
        commodityEntity.setCreateTimestamp(commodityCreateForm.getCreateTimestamp());

        return commodityEntity;

    }


    private CommodityInfo transformTo(CommodityEntity commodityEntity){

        CommodityInfo commodityInfo = new CommodityInfo();

        commodityInfo.setId(commodityEntity.getId());
        commodityInfo.setCode(commodityEntity.getCode());
        commodityInfo.setName(commodityEntity.getName());
        commodityInfo.setImage(commodityEntity.getImage());
        commodityInfo.setPrice(commodityEntity.getPrice());
        commodityInfo.setState(commodityEntity.getState());
        commodityInfo.setCreateTimestamp(commodityEntity.getCreateTimestamp());

        return commodityInfo;

    }
}
